const KEY = {
  TOKEN: "token",
  ROLE: "role",
  NAME: "name",
};
const local = {
  isLogin: false,
  getToken() {
    return localStorage.getItem(KEY.TOKEN);
  },
  getRoles() {
    return localStorage.getItem(KEY.ROLE);
  },
  getName() {
    return localStorage.getItem(KEY.NAME);
  },
  login(name, token, roles) {
    localStorage.setItem(KEY.TOKEN, token);
    localStorage.setItem(KEY.ROLE, roles);
    localStorage.setItem(KEY.NAME, name);
    this.isLogin = true;
  },
  logout() {
    this.isLogin = false;
    localStorage.clear();
  },
  init(func) {
    let token = localStorage.getItem(KEY.TOKEN);
    let name = localStorage.getItem(KEY.NAME);
    let role = localStorage.getItem(KEY.ROLE);
    if (token && name && role) {
      this.isLogin = true;
      func(name);
    }
  },
};
export default local;
